package com.wetao.module_report.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wetao.module_report.R;
import com.wetao.module_report.R2;
import com.wetao.lib_common.bean.response.StudentExamListInfo;
import com.wetao.lib_common.utils.MultiClickUtil;
import com.wetao.lib_common.utils.StringUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ReportMain_v_Adapter extends RecyclerView.Adapter{
    List<StudentExamListInfo.Data.RecordsBean> mData;
    public ReportMain_v_Adapter() {
    }

    public void updateData(List<StudentExamListInfo.Data.RecordsBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == 1) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.report_main_v_item, parent, false);
            return new VHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof VHolder) {
            ((VHolder)holder).bindData(position);
        }
    }

    @Override
    public int getItemCount() {
        if(mData == null) {
            return 0;
        }
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }


    class VHolder extends RecyclerView.ViewHolder{

        @BindView(R2.id.tv_title)
        TextView tv_title;
        @BindView(R2.id.tv_time_tag)
        TextView tv_time_tag;
        @BindView(R2.id.tv_time)
        TextView tv_time;
        @BindView(R2.id.tv_progress)
        TextView tv_progress;



        public VHolder( View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(int position) {
            clickListener(position);
            tv_title.setText(StringUtil.replaceAmp(mData.get(position).getExamName()));

            if(StringUtil.isEmpty(mData.get(position).getFinishTime())) {
                tv_time_tag.setVisibility(View.GONE);
                tv_time.setVisibility(View.GONE);
            }else {
                tv_time_tag.setVisibility(View.VISIBLE);
                tv_time_tag.setText("完成时间：");
                tv_time.setVisibility(View.VISIBLE);
                tv_time.setText(mData.get(position).getFinishTime().toString());
            }

            tv_progress.setText(mData.get(position).getAnswerAmount()  + "/" + mData.get(position).getTotalQuestion() );
        }
        private void clickListener(int position) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null && MultiClickUtil.isFastClick()) {
                        listener.onItemClick(position);
                    }
                }
            });
        }
    }

    //第一步 定义接口
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
    private OnItemClickListener listener;
    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
